use std::collections::HashMap;

use axum::{ extract::Query, Json };
use serde_json::{Value, json};

use crate::{code, file::download, mdb::{gridfs::gridfs_download_base64, gridfs_file::{self, find::get_info}}, query};

use super::upload_gridfs_move::parse_url;
// 查看原始数据
pub async fn look_data(Query(param):Query<Value>) -> Result<Json<Value>, Json<Value>>{
    let id = query::str(&param, "id")?;
    let bucket = query::str(&param, "bucket")?;
    code::data(json!( gridfs_download_base64::get_base64_op(&id, &bucket).await? ) )
}
// 通过 3 个参数返回 base64
pub async fn look_by_id_bucket_type(Query(param):Query<Value>) -> Result<Json<Value>, Json<Value>>{
    let id = query::str(&param, "id")?;
    let r#type = query::str(&param, "type")?;
    let bucket = query::str(&param, "bucket")?;
    code::data(json!( gridfs_download_base64::download(&id, &bucket, &r#type).await? ) )
}
// 上传时回显数据专用
pub async fn look_upload_by_url(Query(param):Query<Value>) -> Result<Json<Value>, Json<Value>>{
    let url = query::str(&param, "url")?;
    let obj = parse_url(&url)?;
    let def = &String::default();
    let id = obj.get("id").unwrap_or(def);
    let bucket = obj.get("bucket").unwrap_or(&def);
    let r#type = obj.get("type").unwrap_or(&def);
    let rs = gridfs_download_base64::download(id, bucket, r#type).await?;
    code::data(json!({ "id": url, "url": rs }))
}
pub async fn look_upload_by_urls(Query(param):Query<Value>) -> Result<Json<Value>, Json<Value>>{
    let url = query::vec_str(&param, "url")?;
    let mut arr:Vec<HashMap<&str, String>> = Vec::new();
    for v in url {
        let mut obj:HashMap<&str, String> = HashMap::new();
        let os = parse_url(&v)?;
        let def = &String::default();
        let id = os.get("id").unwrap_or(def);
        let bucket = os.get("bucket").unwrap_or(&def);
        let r#type = os.get("type").unwrap_or(&def);
        if let Some(f) = gridfs_download_base64::download(id, bucket, r#type).await? {
            obj.insert("id", v);
            obj.insert("url", f);
        }
        if !obj.is_empty(){
            arr.push(obj);
        }
    }
    code::data(json!( arr ) )
}
// 获取文件 base64
pub async fn look_by_url(Query(param):Query<Value>) -> Result<Json<Value>, Json<Value>>{
    let url = query::str(&param, "url")?;
    let obj = parse_url(&url)?;
    let def = &String::default();
    let id = obj.get("id").unwrap_or(def);
    let bucket = obj.get("bucket").unwrap_or(&def);
    let typ = obj.get("type").unwrap_or(&def);
    let v = vec!["pdf","png","jpg","png","ico","svg","bmp"];
    let t = &typ[..];
    if v.contains(&t) {
        let rs = gridfs_download_base64::base64(id, bucket).await?;
        let url = download::base64(rs, id, &typ).await?;
        code::data(json!(url))
    }else{
        let rs = gridfs_download_base64::download(id, bucket, &typ).await?;
        code::data(json!(rs))
    }
}
pub async fn look_by_urls(Query(param):Query<Value>) -> Result<Json<Value>, Json<Value>>{
    let url = query::vec_str(&param, "url")?;
    let mut arr:Vec<String> = Vec::new();
    for v in url {
        let os = parse_url(&v)?;
        let def = &String::default();
        let id = os.get("id").unwrap_or(def);
        let bucket = os.get("bucket").unwrap_or(&def);
        let r#type = os.get("type").unwrap_or(&def);
        if let Some(f) = gridfs_download_base64::download(id, bucket, r#type).await? {
            arr.push(f)
        }
    }
    code::data(json!( arr ) )
}
// 查看文件信息
pub async fn look_info(Query(param):Query<Value>) -> Result<Json<Value>, Json<Value>>{
    let id = query::str(&param, "id")?;
    let bucket_name = query::str(&param, "bucket")?;
    let doc = gridfs_file::find::get_info_json(id, bucket_name).await?;
    code::data(json!(doc))
}
// base64 下载文件
pub async fn mg_download(Query(param):Query<Value>) -> Result<Json<Value>, Json<Value>>{
    let url = query::str(&param, "url")?;
    let obj = parse_url(&url)?;
    let def = &String::default();
    let id = obj.get("id").unwrap_or(def);
    let r#type = obj.get("type").unwrap_or(&def);
    let bucket = obj.get("bucket").unwrap_or(&def);

    let info = get_info(id, bucket).await?;
    let filename = info.filename;

    if let Some(d) = gridfs_download_base64::get_base64_op(&id, &bucket).await?{
        code::data(json!({ "data":d, "type":r#type, "name":filename }))
    }else{
        code::err(url + " 不存在")
    }
}